package Service;
import java.util.ArrayList;
import java.util.List;

import Service.TimetableService;
import Dao.RoomDao;
import Entity.Room;
import Entity.RoomHelp;
import Entity.Timetable;
public class RoomService {
	public List<Room> findAllRoom(){//返回所有机房
		RoomDao dao=new RoomDao();
		return dao.findAllRoom();
	}
	public Room findByRoomid(String roomid){//根据机房id返回机房
		RoomDao dao=new RoomDao();
		return dao.findByRoomid(roomid);
	}        
	public List<Room> findByRoomtype(int roomtype){// 根据机房类型（硬件或软件）返回机房
		RoomDao dao=new RoomDao();
		return dao.findByRoomtype(roomtype);
	}    
	public boolean addRoom(Room room){//添加机房
		RoomDao dao=new RoomDao();
		return dao.addRoom(room);
	}
	public boolean updateRoom(Room room){//修改机房
		RoomDao dao=new RoomDao();
		return dao.updateRoom(room);
	}
	public boolean deleteRoom(Room room){//删除机房
		RoomDao dao=new RoomDao();
		return dao.deleteRoom(room);
	}
	//应该对，但是没看删除机制
	public boolean deleteRoom(String roomid){//删除机房   (根据机房id)
		RoomDao dao=new RoomDao();
		Room r=new Room();
		r.setRoomid(roomid);r.setRoomtype(0);r.setTotal(0);
		return dao.deleteRoom(r);
	}
	
	//新添功能，查找某一时间段内的机房，以及机房的剩余位数
	
	public List<RoomHelp> findRoomByTime(int week,int day,int section){// 根据机房类型（硬件或软件）返回机房
		//首先找出这个时间有课的机房
		TimetableService tSer=new TimetableService();
		RoomService rSer=new RoomService();
		SelectionService sSer=new SelectionService();
		List<Timetable> tables=tSer.findByTime(week, day, section);
		List<Room> rooms=rSer.findAllRoom();
		if(tables==null){
			List<RoomHelp> list = new ArrayList<RoomHelp>();
			for(int i=0;i<rooms.size();i++){
				Room r=rooms.get(i);
				RoomHelp tmp=new RoomHelp();
				tmp.setRoomid(r.getRoomid());tmp.setRoomtype(r.getRoomtype());tmp.setTotal(r.getTotal());
				tmp.setLeftSum(r.getTotal());
				list.add(tmp);
			}
			//根据Selection表单计算剩余机位
			for(int i=0;i<list.size();i++){
				int chosdSum=sSer.findChosedRoomNum(week, day, section, list.get(i).getRoomid());
				list.get(i).setLeftSum(list.get(i).getTotal()-chosdSum);
			}
			return list;
		}
		//if(tables==null) return rooms;
		//两层循环，机房id没有在tables中出现的就是有空的
		List<RoomHelp> list = new ArrayList<RoomHelp>();
		for(int i=0;i<rooms.size();i++){//java 中 String类型不能直接用==比较
			int ok=0;
			for(int j=0;j<tables.size();j++){
				if((rooms.get(i).getRoomid()).equals((tables.get(j).getRoomid()))==false){
					ok++;
				}
			}
			if(ok==tables.size()){
				Room r=rooms.get(i);
				RoomHelp tmp=new RoomHelp();
				tmp.setRoomid(r.getRoomid());tmp.setRoomtype(r.getRoomtype());tmp.setTotal(r.getTotal());
				tmp.setLeftSum(r.getTotal());
				list.add(tmp);
			}
		}
		//根据Selection表单计算剩余机位
		for(int i=0;i<list.size();i++){
			int chosdSum=sSer.findChosedRoomNum(week, day, section, list.get(i).getRoomid());
			list.get(i).setLeftSum(list.get(i).getTotal()-chosdSum);
		}
		return list;
	}    
	
	
	
public static void main(String[] args) {
		//RoomDao dao=new RoomDao();
		//List<Room>rooms=dao.findByRoomtype(1);
		RoomService ser=new RoomService();
		List<RoomHelp> rooms=ser.findRoomByTime(12, 2, 1);
		for(int i=0;i<rooms.size();i++){
			System.out.println("roomId"+rooms.get(i).getRoomid()+"   roomtype:"+rooms.get(i).getRoomtype()+"   total:"+rooms.get(i).getTotal()+" leftSum:"+rooms.get(i).getLeftSum());
		}
	}
}
